package com.example.democrud.common.handler;

import com.example.democrud.ResponseVO;
import com.example.democrud.common.exception.DBCreateException;
import com.example.democrud.common.exception.DBDeleteException;
import com.example.democrud.common.exception.DBRetrieveException;
import com.example.democrud.common.exception.DBUpdateException;
import com.example.democrud.enums.ResponseEnum;
import lombok.extern.slf4j.Slf4j;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;


/**
 * @ClassName ExceptionHandler
 * @Author Administrator
 * @Date 2020/4/23   11:28
 */
@Slf4j
@RestControllerAdvice
public class CurrentExceptionHandler  {
    @ExceptionHandler(DBRetrieveException.class)
    public ResponseVO exception(DBRetrieveException e){
        return ResponseVO.error(ResponseEnum.DB_RETRIEVE_ERROR);
    }

    @ExceptionHandler(DBUpdateException.class)
    public ResponseVO exception(DBUpdateException e){
        return ResponseVO.error(ResponseEnum.DB_UPDATE_ERROR);
    }

    @ExceptionHandler(DBCreateException.class)
    public ResponseVO exception(DBCreateException e){
        return ResponseVO.error(ResponseEnum.DB_CREATE_ERROR);
    }

    @ExceptionHandler(DBDeleteException.class)
    public ResponseVO exception(DBDeleteException e){
        return ResponseVO.error(ResponseEnum.DB_DELETE_ERROR);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseVO exception(HttpRequestMethodNotSupportedException e){
        return ResponseVO.error(ResponseEnum.HTTP_REQUEST_METHOD_ERRER);
    }
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseVO exception(HttpMessageNotReadableException e){
        return ResponseVO.error(ResponseEnum.HTTP_REQUEST_METHOD_ERRER);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseVO exception(MethodArgumentNotValidException e){
        BindingResult bindingResult = e.getBindingResult();
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        StringBuilder errors = new StringBuilder();
        for (int i=0;i<allErrors.size();i++) {
            if(allErrors.get(i)!=null && allErrors.get(i).getDefaultMessage()!=null) {
                if(i>0) errors.append(";");
                errors.append(allErrors.get(i).getDefaultMessage());
            }
        }
        return ResponseVO.error(ResponseEnum.ERROR, String.valueOf(errors));
    }


    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public ResponseVO exception(MethodArgumentTypeMismatchException e){
        log.error("传入参数类型错误：" +e.getMessage());
        return ResponseVO.error(ResponseEnum.ERROR, "传入参数类型错误："+e.getMessage());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseVO exception(DuplicateKeyException e){
        return ResponseVO.error(ResponseEnum.DB_CREATE_ERROR, "用户名已经存在");
    }


}
